/*
 * Copyright (c) 2022  by Junzhao
 * THIS FILE IS PART OF JAVASE Song Huongkang PROJECT
 * All Rights Reserved.
 */

package 泛型练习;

import java.util.*;

/**
 * @Description
 * @Author 俊昭
 * @Date 2022/4/22
 */
public class DAO<T> {
    private Map<String,T> map = new HashMap<>();

    /*
    保存对象到map
     */
    public void save(String id,T entity){
        if (map.containsKey(id)){
            throw new RuntimeException("此用户已存在！");
        }else{
            map.put(id,entity);
        }
    }

    /*
    从map中获取id对应的对象
     */
    public T get(String id){
        return map.get(id);
    }
    /*
    替换map中key为id的内容 改为entity
     */
    public void update(String id,T entity){
        if (map.containsKey(id)){
            map.put(id,entity);
        }else{
            throw new RuntimeException("没有指定的id");
        }
    }
    /*
    返回map中存放的的对象
     */
    public List<T> list(){
        Collection<T> values = map.values();

        List<T> l = new ArrayList<>(values);
        return l;
    }
    /*
    删除指定id对象
     */
    public void delete(String id){
        if (map.containsKey(id)){
            map.remove(id);
        }else{
            throw new RuntimeException("没有指定的id");
        }
    }
}
